﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    //链表节点
    public class Node<T>
    {
        public Node<T> Next { get; set; }
        public T Data { get; set; }

        public Node(T t)
        {
            Next = null;
            Data = t;
        }
    }

    //泛型链表类
    public class GenericList<T>
    {
        private Node<T> head;
        private Node<T> tail;

        public GenericList()
        {
            tail = head = null;
        }

        public Node<T> Head
        {
            get => head;
        }

        public void Add(T t)
        {
            Node<T> n = new Node<T>(t);
            if (tail == null)
            {
                head = tail = n;
            }
            else
            {
                tail.Next = n;
                tail = n;
            }
        }

        public void ForEach(Action<T> action)
        {
            for (Node<T> n = head; n != null; n = n.Next)
            {
                action(n.Data);
            }
        }
    }

    internal class Program
    {
        static void Main(string[] args)
        {
            GenericList<int> genericList = new GenericList<int>();
            for (int x = 0; x < 10; x++)
            {
                genericList.Add(x);
            }
            int max = genericList.Head.Data;//最大值
            int min = genericList.Head.Data;//最小值
            int sum = 0;//和
            genericList.ForEach(x => Console.WriteLine(x));//打印链表元素
            genericList.ForEach(x => { if (x > max) max = x; });//求最大值
            genericList.ForEach(x => { if (x < min) min = x; });//求最小值
            genericList.ForEach(x => sum += x);//求和
            Console.WriteLine("最大值为:" + max);//输出最大值
            Console.WriteLine("最小值为:" + min);//输出最小值
            Console.WriteLine("和为:" + sum);//输出和
        }
    }
}
